<template>
  <div class="users">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户管理</span>
          <el-button type="primary">新增用户</el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="searchForm.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="searchForm.role" placeholder="请选择角色">
            <el-option label="管理员" value="admin" />
            <el-option label="采购员" value="purchaser" />
            <el-option label="仓库管理员" value="warehouse" />
            <el-option label="普通用户" value="user" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="正常" value="active" />
            <el-option label="禁用" value="inactive" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格区域 -->
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="role" label="角色" width="120">
          <template #default="scope">
            <el-tag :type="getRoleType(scope.row.role)">
              {{ getRoleText(scope.row.role) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="department" label="部门" width="120" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="phone" label="手机号" width="120" />
        <el-table-column prop="lastLogin" label="最后登录" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'active' ? 'success' : 'danger'">
              {{ scope.row.status === 'active' ? '正常' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="scope">
            <el-button type="primary" link>编辑</el-button>
            <el-button type="primary" link>重置密码</el-button>
            <el-button type="primary" link>分配角色</el-button>
            <el-button type="danger" link>禁用</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'

const searchForm = reactive({
  username: '',
  name: '',
  role: '',
  status: ''
})

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

const tableData = ref([
  {
    username: 'admin',
    name: '管理员',
    role: 'admin',
    department: '系统管理部',
    email: 'admin@example.com',
    phone: '13800138000',
    lastLogin: '2024-03-17 10:00:00',
    status: 'active'
  },
  {
    username: 'purchaser1',
    name: '张三',
    role: 'purchaser',
    department: '采购部',
    email: 'zhangsan@example.com',
    phone: '13800138001',
    lastLogin: '2024-03-17 09:30:00',
    status: 'active'
  },
  {
    username: 'warehouse1',
    name: '李四',
    role: 'warehouse',
    department: '仓库部',
    email: 'lisi@example.com',
    phone: '13800138002',
    lastLogin: '2024-03-17 09:00:00',
    status: 'active'
  },
  {
    username: 'user1',
    name: '王五',
    role: 'user',
    department: '销售部',
    email: 'wangwu@example.com',
    phone: '13800138003',
    lastLogin: '2024-03-17 08:30:00',
    status: 'inactive'
  }
])

const getRoleType = (role) => {
  const types = {
    admin: 'danger',
    purchaser: 'warning',
    warehouse: 'success',
    user: 'info'
  }
  return types[role] || 'info'
}

const getRoleText = (role) => {
  const texts = {
    admin: '管理员',
    purchaser: '采购员',
    warehouse: '仓库管理员',
    user: '普通用户'
  }
  return texts[role] || role
}

const handleSearch = () => {
  console.log('搜索条件：', searchForm)
}

const resetSearch = () => {
  searchForm.username = ''
  searchForm.name = ''
  searchForm.role = ''
  searchForm.status = ''
}
</script>

<style scoped>
.users {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 